package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.PromotionCard;
import com.ericom.service.GenericManager;

public class PromotionCardAction extends BaseAction {

	private GenericManager<PromotionCard, Long> promotionCardManager;
	private List promotionCards;
	private PromotionCard promotionCard;
	private Long id;

	public PromotionCard getPromotionCard() {
		return promotionCard;
	}

	public void setPromotionCard(PromotionCard promotionCard) {
		this.promotionCard = promotionCard;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPromotionCardManager(
			GenericManager<PromotionCard, Long> promotionCardManager) {
		this.promotionCardManager = promotionCardManager;
	}

	public String list() {
		promotionCards = promotionCardManager.getAll();
		return SUCCESS;
	}

	public List getPromotionCards() {
		return promotionCards;
	}

	public String delete() {
		promotionCardManager.remove(promotionCard.getId());
		this.saveMessage(getText("promotionCard.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			promotionCard = promotionCardManager.get(id);
		} else {
			promotionCard = new PromotionCard();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (promotionCard.getId() == null);
		promotionCard = promotionCardManager.save(promotionCard);

		String key = (isNew) ? "promotionCard.added" : "promotionCard.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
